<?php
/*
    Copyright (C) 2008 - 2010 Inanta Martsanto
    
    This file is part of Koma.

    Koma is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Koma is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Koma.  If not, see <http://www.gnu.org/licenses/>.
    
    Inanta Martsanto (inanta@inationsoft.com)
*/

abstract class KomaArray extends KomaObject
{
    protected $_array = array();
    
    function __construct()
    {
        $this->createProperties(array('Count' => 0));
    }
    
    protected function add($value)
    {
        $this->_array[$this->Count] = $value;
        $this->Count++;
    }
    
    protected function addRange($values)
    {
        foreach($values as $value)
        {
            $this->add($value);
        }
    }
    
    protected function insert($index, $value)
    {
        $temp = array();
        $bound = 0;
        
        foreach($this->_array as $item)
        {
            if($bound == $index) { $temp[] = $value; $this->Count++; }

            $temp[] =  $item;
            $bound++;
        }
        
        if($bound == 0) { $temp[0] = $value; $this->Count++; }
        
        $this->_array = $temp;
    }
    
    protected function insertRange($index, $values)
    {
        $temp = array();
        $bound = 0;

        foreach($this->_array as $item)
        {
            if($bound == $index)
            {
                foreach($values as $value)
                {
                    $temp[] = $value;
                    $this->Count++;
                }
            }
            
            $temp[] =  $item;
            $bound++;
        }
        
        if($bound == 0)
        {
                foreach($values as $value)
                {
                    $temp[] = $value;
                    $this->Count++;
                }
        }
        
        $this->_array = $temp;
    }
    
    protected function removeAt($index)
    {
        $temp = array();
        
        for($i = 0; $i < $this->Count; $i++)
        {
            if($i != $index) $temp[] = $this->_array[$i];
            else $this->Count--;
        }
        
        $this->_array = $temp;
    }
    
    protected function remove($value)
    {   
        for($i = 0; $i < $this->Count; $i++)
        {
            if($this->_array[$i] === $value) { $this->removeAt($i); break ; }
        }
    }
    
    protected function removeAll($value)
    {
        $temp = array();
        $this->Count = 0;
        
        foreach($this->_array as $item)
        {
            if($item !== $value) { $temp[] = $item; $this->Count++; }
        }
        
        $this->_array = $temp;
    }
    
    function toArray()
    {
        return $this->_array;
    }
    
    function sort()
    {
        @sort($this->_array);
    }
    
    function reverse()
    {
        $this->_array = @array_reverse($this->_array);
    }
    
    function clear()
    {
        $this->_array = array();
    }
}
?>